define(['./../../initial'],
    function () {

        var app = angular.module('App.home');

        app.factory('reportDateFac', ['$filter', function ($filter) {
            var fac = {
                startDate: '',
                endDate: '',
                pointStartDate: '',
                pointEndDate: ''
            };
            //初始化开始和结束时间,默认为当前天和一个月之前
            if (!fac.endDate) {
                var date = new Date();
                fac.endDate = $filter('date')(date, 'yyyy-MM-dd');
                fac.startDate = $filter('date')(date.setMonth(date.getMonth() - 1), 'yyyy-MM-dd');
                date = new Date();
                fac.pointStartDate = $filter('date')(date.setFullYear(date.getFullYear() - 1), 'yyyy-MM-dd');
                fac.pointEndDate = fac.endDate;
            }
            return fac;
        }]);

        app.controller('report.projectCtrl', ['$timeout', '$scope', '$rootScope', '$state', '$stateParams', 'curPrj', 'curStatus', 'reportDateFac', 'chartFactory', 'Project', '$filter', '$compile', function ($timeout, $scope, $rootScope, $state, $stateParams, curPrj, curStatus, reportDateFac, chartFactory, Project, $filter, $compile) {
            //工况
            $scope.curPrj = curPrj;
            $scope.curStatus = curStatus;
            $scope.prjName = curPrj.name.length > 16 ? curPrj.name.slice(0, 16) + '...' : curPrj.name;
            
            //获取所有的测项和位置
            var monitorPoints = [];
            curPrj.monitorPoints.forEach(function (moniorPoint) {
                moniorPoint.locationPoints.forEach(function (lp, index) {
                    monitorPoints.push({
                        monitorType: moniorPoint.monitorType,
                        location: lp.location,
                        points: lp.points,
                        count: index === 0 ? moniorPoint.locationPoints.length : 0
                    })
                });
            });
            $scope.monitorPoints = monitorPoints;
            
            //初始化日期
            $scope.startDate = reportDateFac.startDate;
            $scope.endDate = reportDateFac.endDate;
            $scope.pointStartDate = reportDateFac.pointStartDate;
            $scope.pointEndDate = reportDateFac.pointEndDate;
            //初始化日历
            $timeout(function () {
                $('#startDate').popup({ on: 'click', position: "bottom left" });
                $('#endDate').popup({ on: 'click', position: "bottom left" });
            }, 1000);

            $scope.chooseDate = function (opts) {
                $('#' + opts.calendarId).popup('hide');
                $scope[opts.calendarId] = opts.date; //设置日期
                reportDateFac[opts.calendarId] = opts.date;//设置日期
                if (opts.calendarId === 'endDate') {
                    $scope.pointEndDate = opts.date;
                    reportDateFac.pointEndDate = opts.date;
                }
            };

            $scope.busy = true;
            $scope.noData = false;//所有的测项在阶段时间内完全没数据
            var chartWidth = $('#changes').width();
            $scope.search = function () {
                var finish = 0;//当前完成查询w的数量
                $scope.busy = true;
                //获取所有的测量和位置的变化量
                $scope.monitorPoints.forEach(function (item, index) {
                    curPrj.getRecentChanges(item.monitorType, item.location, reportDateFac.startDate, reportDateFac.endDate)
                        .then((function (dex) {
                            return function (data) {
                                finish++;
                                $scope.monitorPoints[dex].noData = data.curValues.length === 0;
                                $scope.monitorPoints[dex].changes = data;
                                //为两次时间相同情况下的speed做一个特殊处理
                                if (!$scope.monitorPoints[dex].noData && data.maxPointThisTime.speed === null) {
                                    data.maxPointThisTime.speed = 0;
                                }
                                //加载特殊位置信息
                                parseRemarkToChangeData($scope.monitorPoints[dex], $scope.monitorPoints[dex].changes);
                                //绘制图表
                                $timeout(function () {
                                    !$scope.monitorPoints[dex].noData && chartFactory.createPointsChartByLocationSummary('#chart' + dex, data, chartWidth);
                                }, 300);
                                if (finish === $scope.monitorPoints.length) {//最后一个
                                    $scope.busy = false;
                                    $scope.noData = $scope.monitorPoints.every(function (item) {
                                        return item.noData;
                                    });
                                }
                            };
                        })(index));
                });
                //防止没点还在等待查询
                if ($scope.monitorPoints.length === 0) {
                    $scope.busy = false;
                }
            };
            $scope.search();
            
            //加载特殊位置信息
            function parseRemarkToChangeData(monitorPoints, change) {
                change.lastValues && change.lastValues.length > 0 && change.lastValues.forEach(function (item, index) {
                    item.remark = monitorPoints.points[index].remark || '';
                });
            }
            
            //查询单点信息并打开模态框
            $scope.singlePointQuery = function (ptName) {
                if (!ptName) return;
                $scope.ptName = ptName;
                chartFactory.destroy('#singleChart');

                var div = document.getElementById('singlePointChart');
                if (div) {
                    div.remove();
                }
                div = document.createElement('div');
                div.setAttribute('data-single-Point-Chart', '');
                div.setAttribute('data-cur-prj', 'curPrj');
                div.setAttribute('data-pt-Name', 'ptName');
                div.setAttribute('data-point-Start-Date', 'pointStartDate');
                div.setAttribute('data-point-End-Date', 'pointEndDate');
                document.body.appendChild(div);
                $compile(angular.element(div))($scope);
            };
            $scope.ptName = "";
            //侦听图表上的单点点击事件
            $scope.$on('chartPointClick', function (event, ptName) {
                $scope.singlePointQuery(ptName);
            });

        }]);
    });
